package fdts.data.io;

/********************************************************
 * FDTS SS 2012. Calendar Layouter and iCalendar Imorter*
 * ******************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.ParseException;

import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.data.ParserException;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.DateTime;

import prefuse.data.io.DataIOException;
import timeBench.calendar.Granularity;
import timeBench.data.TemporalDataException;
import timeBench.data.TemporalDataset;
import timeBench.data.io.TemporalDatasetReader;

/**
 * this class importes a ics file in a certain period
 * and for a specific x-axis granularity and y-axis granularity.
 * 
 * @author agnieszka wojdecka and andreas hoermann
 *
 */
public class iCalendarImporter implements TemporalDatasetReader{

	/**
	 * starting point of period, in local time format (see iCalendar specification)
	 */
	private String from;
	/**
	 * endpoint of period, in local time format (see iCalendar specification)
	 */
	private String to;
	/**
	 * parser for iCalendar file
	 */
	public iCalendarParser parser;
	/**
	 * granularity of x axis
	 */
	private Granularity xGranularity;
	/**
	 * granularity of y axis
	 */
	private Granularity yGranularity;
	/**
	 * milliseconds of the current time
	 */
	private long today;
	
	/**
	 * the constructor
	 * @param from starting point of period, in local time format (see iCalendar specification)
	 * @param to endpoint of period, in local time format (see iCalendar specification)
	 * @param xGranularity granularity of x axis
	 * @param yGranularity granularity of y axis
	 * @param today milliseconds of the current time
	 */
	public iCalendarImporter(String from, String to,Granularity xGranularity,Granularity yGranularity,long today){
		this.from = from;
		this.to = to;
		parser = new iCalendarParser();
		this.xGranularity = xGranularity;
		this.yGranularity = yGranularity;
		this.today = today;
	}
	
	@Override
	public TemporalDataset readData(String arg0) throws DataIOException,
			TemporalDataException {
		File f = new File(arg0);
		return readData(f);
	}

	@Override
	public TemporalDataset readData(URL arg0) throws DataIOException,
			TemporalDataException {
		System.out.println( "Is not implemented yet." );
		return null;
	}

	/**
	 * this method imports the ics file.
	 * @param file ics file
	 */
	@Override
	public TemporalDataset readData(File file) throws DataIOException,
			TemporalDataException {
		
		Calendar calendar = null;
		FileInputStream fin;
		try {
			fin = new FileInputStream(file);
			CalendarBuilder builder = new CalendarBuilder();
			calendar = builder.build(fin);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserException e) {
			e.printStackTrace();
		}
		
		DateTime from = null;
		DateTime to = null;
		try {
			from = new DateTime(this.from);
			to  = new DateTime(this.to);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return parser.parse(calendar,from,to,xGranularity,yGranularity,today);
	}

	@Override
	public TemporalDataset readData(InputStream arg0) throws DataIOException,
			TemporalDataException {
		System.out.println( "Is not implemented yet." );
		return null;
	}
}